#include "IR.h"

// 使用定时器实现微秒延时
#define DLY_TIM_Handle &htim1
void HAL_Delay_us(uint16_t nus)
{
  __HAL_TIM_SET_COUNTER(DLY_TIM_Handle, 0);
  __HAL_TIM_ENABLE(DLY_TIM_Handle);
  while (__HAL_TIM_GET_COUNTER(DLY_TIM_Handle) < nus) {
  }
  __HAL_TIM_DISABLE(DLY_TIM_Handle);
}

//红外
void Send_L()//发送L码
{
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,947);
  HAL_Delay_us(4500);
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,0);
  HAL_Delay_us(4500);
}

void Send_S()//发送S码
{
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,947);
  HAL_Delay_us(550);
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,0);
  HAL_Delay_us(5220);
}
void Send_Stop()//发送stop码
{
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,947);
  HAL_Delay_us(550);
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,0);
  HAL_Delay_us(20000);
}
void Send_1()//发送高电平
{
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,947);
  HAL_Delay_us(550);
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,0);
  HAL_Delay_us(1680);
}
void Send_0()//发送低电平
{
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,947);
  HAL_Delay_us(550);
  __HAL_TIM_SetCompare(&htim2,TIM_CHANNEL_2,0);
  HAL_Delay_us(550);
}
void Send_Code(uint8_t data)//发送编码
{
  for (int i = 7; i >=0; i--) {
    if (data&(1<<i)) {
      Send_1();
    } else {
      Send_0();
    }
  }
}
/*
在该函数中由于一般情况下A是固定的所以我们给A固定的值、
B控制的是风速高三位控制、
C控制的是温度高四位、
模式四位后的两位
*/
void Air_Contorl(uint8_t B,uint8_t C)
{
  Send_L();
  Send_Code(0xB2);
  Send_Code(~0xB2);
  Send_Code(B);
  Send_Code(~B);
  Send_Code(C);
  Send_Code(~C);
  Send_S();

  Send_L();
  Send_Code(0xB2);
  Send_Code(~0xB2);
  Send_Code(B);
  Send_Code(~B);
  Send_Code(C);//制冷26度
  Send_Code(~C);
  Send_Stop();
}
